﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Snake
    {

        public SnakeNode head;
        public SnakeNode[] body;
        public SnakeNode tail;
        public Snake() {
                SnakeNode head = new SnakeNode();
                head.x = 50;
                head.y = 50;
                head.next = new SnakeNode();
                //node.next = new SnakeNode();
                head.next.x = 45;
                head.next.y = 50;
                head.next.next = new SnakeNode();
                head.next.next.x = 40;
                head.next.next.y = 50;
                head.next.next.next = new SnakeNode();
                head.next.next.next.x = 35;
                head.next.next.next.y = 50;
                head.next.next.next.next = new SnakeNode();
                head.next.next.next.next.x = 30;
                head.next.next.next.next.y = 50;
                //head.next.next.next.next.next = new SnakeNode();
            /*
                for (int i = 0; i < length; i++) {
                    node.x = 50 - i * 5;
                    node.y = 50 - i * 5;
                    node.next = new SnakeNode();
                    node = node.next;
                }*/
        }
        public Snake(SnakeNode head, SnakeNode[] body,SnakeNode tail)
        {
            this.head = head;
            this.body = body;
            this.tail = tail;
           
            this.head.next = body[0];
            int i;
            for ( i = 0; i < body.Length - 2; i++)
            {
                body[i].next = body[i+1];
            }
            body[i+1].next = tail;
        }

        //appeller juste apres avoir manger, pas pendant
        public void eat(int x, int y, String direction)
        {
            SnakeNode node = new SnakeNode (this.head.x , this.head.y , false, false);
            if (direction == "top"){
                y += 5;
            }
            if (direction == "bottom"){
                y -= 5;
            }
            if (direction == "left"){
                x -= 5;
            }
            if (direction == "right"){
                x += 5;
            }
            this.head.x = x;
            this.head.y = y;
            node.next = this.head.next;
            this.head.next = node;
        }

        public void move(int x , int y, int length)
        {
            this.tail.x = this.body[length-1].x;
            this.tail.y = this.body[length-1].y;
            for (int i = length-1 ; i > 0 ; i --) 
            {
                this.body[i].x = this.body[i-1].x;
                this.body[i].y = this.body[i-1].y;
            }
            this.head.x = x;
            this.head.y = y;
        }
    }
}
